package com.amor.ui

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.net.Uri
import android.net.http.SslError
import android.os.SystemClock
import android.view.ViewGroup
import android.webkit.*
import com.amor.app.BaseActivity
import com.amor.demo.web1.R
import com.amor.demo.web1.databinding.VWebBinding
import com.amor.util.WebResourceAssetUtil
import com.amor.vm.BaseViewModel
import com.hy.frame.ui.ToolbarUI
import com.hy.frame.util.LogUtil

abstract class BaseWebActivity : BaseActivity<VWebBinding, BaseViewModel>() {
    var toolbar: ToolbarUI? = null
    var mTitle: String? = null
    var lastPageStartTime = 0L
    var initStartTime = 0L
    var TAG: String = javaClass.simpleName

    override val layoutId: Int = R.layout.v_web

    override fun initView() {
        this.toolbar = com.amor.widget.ToolbarUI(context, { finish() }, null).setLeftIcon(R.mipmap.ic_back).apply {
            setTitle(R.string.empty)
            build(this@BaseWebActivity, mBinding?.layout)
        }
    }

    override fun initData() {

    }

    fun showConsole(msg: String?) {
        if (msg.isNullOrBlank())
            return
        mBinding?.txtConsole?.append(msg)
    }

    @SuppressLint("SetJavaScriptEnabled")
    fun initWebSettings(settings: WebSettings, mode: Int) {
        settings.apply {
            // 设置WebView支持JavaScript
            javaScriptEnabled = true
            defaultTextEncodingName = "utf-8" //默认UTF-8
            setSupportZoom(false) //设置WebView是否支持使用屏幕控件或手势进行缩放，默认是true，支持缩放
            displayZoomControls = false //设置WebView使用内置缩放机制时，是否展现在屏幕缩放控件上，默认true，展现在控件上。
            loadWithOverviewMode = true//是否允许WebView度超出以概览的方式载入页面，默认false。即缩小内容以适应屏幕宽度。该项设置在内容宽度超出WebView控件的宽度时生效，例如当getUseWideViewPort() 返回true时。
            //mediaPlaybackRequiresUserGesture = true //设置WebView是否通过手势触发播放媒体，默认是true，需要手势触发
            //builtInZoomControls = false //设置WebView是否使用其内置的变焦机制，该机制集合屏幕缩放控件使用，默认是false，不使用内置变焦机制
            allowFileAccess = true //设置在WebView内部是否允许访问文件，默认允许访问。
            allowContentAccess = true //是否允许在WebView中访问内容URL（Content Url），默认允许。内容Url访问允许WebView从安装在系统中的内容提供者载入内容。
            saveFormData = false
            domStorageEnabled = true
            //webview 从Lollipop(5.0)开始 webview默认不允许混合模式，https当中不能加载http资源，如果要加载，需单独设置开启。
            mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            blockNetworkImage = false
            allowUniversalAccessFromFileURLs = true
            cacheMode = mode
        }
    }

    private var mWeb: WebView? = null
    fun initClient(web: WebView) {
        this.mWeb = web
        web.webViewClient = object : WebViewClient() {
            private var loadError = false
            override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
                LogUtil.d(TAG, "onReceivedSslError error=$error")
                handler?.proceed()
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                LogUtil.d(TAG, "shouldOverrideUrlLoading url=$url")
                //阻止协议跳转
                if (url.isNullOrEmpty()) return true
                if (url.startsWith("http://") || url.startsWith("https://"))
                    view?.loadUrl(url)
                return true
            }

            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                return super.shouldOverrideUrlLoading(view, request)
            }

            override fun onLoadResource(view: WebView?, url: String?) {
                LogUtil.d(TAG, "onLoadResource url=$url")
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                LogUtil.d(TAG, "onPageStarted url=$url")
                lastPageStartTime = SystemClock.elapsedRealtime();
                loadError = false
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                LogUtil.d(TAG, "onPageFinished url=$url")
                if (lastPageStartTime > 0) {
                    LogUtil.d(TAG, "onPageFinished time=%sms".format(SystemClock.elapsedRealtime() - lastPageStartTime))
                    showConsole("页面加载耗时=%sms\n".format(SystemClock.elapsedRealtime() - lastPageStartTime))
                    lastPageStartTime = 0
                }
//                if (loadError) {
//                    view?.post {
//                        //loading?.showError("网络异常，请稍候重试")
//                    }
//                    return
//                }
//                if (loadAgain) return
//                view?.post {
//                    loadAgain = true
//                    loading?.hide()
//                }
            }

            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                if (request == null) return
                LogUtil.d(TAG, "onReceivedError url=" + request.url)
                if (request.isForMainFrame) {
                    loadError = true
                }
            }

            override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
                //静态资源替换
                val url = request.url.toString()
                if (url.endsWith(".gif") || url.endsWith(".jpg") || url.endsWith(".png") || url.endsWith(".js")) {
                    return WebResourceAssetUtil.find(url)
                }
                return null
            }
        }
        web.webChromeClient = object : WebChromeClient() {
            override fun onJsAlert(view: WebView?, url: String?, message: String?, result: JsResult?): Boolean {
                view?.post {
                    showToast(message)
                }
                result?.confirm()
                return true
            }

            override fun onCloseWindow(window: WebView?) {
                onBackPressed()
            }

            override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
                LogUtil.d(TAG, "msg==%s,line=%s,id=%s".format(consoleMessage?.message(), consoleMessage?.lineNumber(), consoleMessage?.sourceId()))
                return true
            }

            //For Android API >= 21 (5.0 OS)
            override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
                if (filePathCallback != null)
                    webView?.post {
                        //showFileChooser(filePathCallback)
                    }
                return true
            }

            override fun onReceivedTitle(view: WebView?, title: String?) {
                if (!mTitle.isNullOrEmpty()) return
                view?.post {
                    if (!title.isNullOrEmpty() && !title.contains("//"))
                        toolbar?.setTitle(title)
                }
            }

        }

    }

    override fun onBackPressed() {
        if (mWeb != null && mWeb!!.canGoBack()) {
            mWeb!!.goBack()
            return
        }
        super.onBackPressed()

    }
}